﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Threading;
using MediaBrowser.Entities;
using MediaBrowser.IServices;

namespace MediaBrowser.Gui.ViewModels
{
    /// <summary>
    /// View Model Page Base
    /// </summary>
    public abstract class PageViewModelBase : ViewModelBase
    {
        public abstract string Name { get; }

        #region Member Fields

        protected IMediaManager service;
        protected ObservableCollection<Media> medias;
        protected ICollectionView mediasView;
        protected Media newMedia;
        protected readonly Dispatcher dispatcher;

        #endregion

        #region Properties

        public ObservableCollection<Media> Medias
        {
            get
            {
                return medias;
            }
            set
            {
                medias = value;
                OnPropertyChanged("Medias");
            }
        }

        public Media CurrentMedia
        {
            get
            {
                if (mediasView != null)
                {
                    return mediasView.CurrentItem as Media;
                }

                return null;
            }
        }

        public Media NewMedia
        {
            get
            {
                return newMedia;
            }
            set
            {
                newMedia = value;
                OnPropertyChanged("NewMedia");
            }
        }

        public string MediaLookFor
        {
            set
            {
                mediasView.Filter = item =>
                {
                    if (item as Media != null)
                    {
                        if ((item as Media).Name.ToUpper().Contains(value.ToUpper()))
                        {
                            return true;
                        }
                    }

                    return false;
                };
            }
        }

        #endregion

        public PageViewModelBase(IMediaManager service)
            : base()
        {
            this.service = service;
            if (Application.Current != null)
            {
                dispatcher = Application.Current.Dispatcher;
            }
            else
            {
                //pour les test unitaires
                dispatcher = Dispatcher.CurrentDispatcher;
            }

        }

    }
}
